package com.lyl.novel.controller;

import com.github.pagehelper.PageInfo;
import com.lyl.example.common.annotation.IgnoreResponseWrapper;
import com.lyl.novel.model.dto.TaskDTO;
import com.lyl.novel.model.dto.TaskListDTO;
import com.lyl.novel.model.entity.Task;
import com.lyl.novel.service.TaskService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.Set;

/**
 * @author Mr.Luo
 * @date 2022-08-23 10:14
 * @desc 定时任务
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/task")
public class TaskController {
    private final TaskService taskService;

    /**
     * 添加任务
     * @param dto dto
     * @return true/false
     */
    @PostMapping("/add")
    public boolean addJob(@Valid @RequestBody TaskDTO dto){
        return taskService.addJob(dto);
    }

    /**
     * 暂停任务
     * @param id 任务id
     * @return true/false
     */
    @PostMapping("/stop/{id:\\d+}")
    public boolean stopJob(@PathVariable("id") Long id){
        return taskService.stopJob(id);
    }

    /**
     * 恢复任务
     * @param id id
     * @return true/false
     */
    @PostMapping("/resume/{id:\\d+}")
    public boolean resumeJob(@PathVariable("id") Long id){
        return taskService.resumeJob(id);
    }

    /**
     * 运行一次任务
     * @param id id
     * @return true/false
     */
    @PostMapping("/runOnce/{id:\\d+}")
    public boolean runOnce(@PathVariable("id") Long id){
        return taskService.runOnce(id);
    }

    /**
     * 根据id删除任务
     * @param id id
     * @return true/false
     */
    @DeleteMapping("/delete/{id:\\d+}")
    public boolean delJob(@PathVariable("id") Long id){
        return taskService.delJob(id);
    }

    /**
     * 分页查询定时任务列表
     * @param pageNo 页码
     * @param pageSize 每页记录数
     * @param dto dto
     * @return list
     */
    @GetMapping("/list/{pageNo:\\d+}/{pageSize:\\d+}")
    public PageInfo<Task> pageList(@PathVariable Integer pageNo,
                                   @PathVariable Integer pageSize,
                                   @Valid TaskListDTO dto){
        return taskService.pageList(pageNo,pageSize,dto);
    }

    /**
     * 获取所有的任务class列表
     * @return set
     */
    @GetMapping("/class/list")
    public Set<String> getJobClassList(){
        return taskService.getJobClassList();
    }

    /**
     * 下载excel
     * @param pageNo 页码
     * @param pageSize 每页记录数
     */
    @GetMapping("/downloadExcel/{pageNo:\\d+}/{pageSize:\\d+}")
    @IgnoreResponseWrapper
    public void downloadExcel(@PathVariable Integer pageNo,
                                  @PathVariable Integer pageSize) throws IOException {
        taskService.downloadExcel(pageNo,pageSize);
    }

}
